/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import ProgramUtils.Usertask;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author TeslaSolari
 */
public class login {

    public void runGUI() {
        try {
            loginC frame = new loginC();
            frame.setSize(300, 100);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
    }

    public void hideGUI() {
      loginC frame = new loginC();
      frame.setVisible(false);
    }
}

class loginC extends JFrame implements ActionListener {

    JButton login;
    JButton register;
    JPanel panel;
    JLabel label1, label2;
    final JTextField user, pass;

    loginC() {
        label1 = new JLabel();
        label1.setText("Username:");
        user = new JTextField(15);
        label2 = new JLabel();
        label2.setText("Password:");
        pass = new JPasswordField(15);
        //this.setLayout(new BorderLayout());

        login = new JButton("login");
        register = new JButton("register");

        panel = new JPanel(new GridLayout(3, 1));
        panel.add(label1);
        panel.add(user);
        panel.add(label2);
        panel.add(pass);
        panel.add(login);
        panel.add(register);
        add(panel, BorderLayout.CENTER);
        login.addActionListener(this);
        register.addActionListener(this);
        setTitle("LOGIN FORM");
    }

    public void actionPerformed(ActionEvent e) {
        String btp = e.getActionCommand();
//        System.out.println(btp);
        switch (btp) {
            case "register":
                Register obj = new Register();
                System.out.println(btp);
                try {
                    obj.runGUI();
                } catch (SQLException ex) {
                    Logger.getLogger(loginC.class.getName()).log(Level.SEVERE, null, ex);
                }
                break;

            case "login":
                String userN = user.getText();
                String userP = pass.getText();
                Usertask task = new Usertask();
                try {
                    if (task.login(userN, userP)) {
                        JOptionPane.showMessageDialog(rootPane, "Welcome " + userN);
                    } else {
                        JOptionPane.showMessageDialog(rootPane, "Incorect Username Or password");
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(loginC.class.getName()).log(Level.SEVERE, null, ex);
                }
                break;
        }




    }
}
